package monolithicapp

import (
	"log/slog"
	"os"

	"gopkg.in/yaml.v3"
)

type RedisConfig struct {
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	Password string `yaml:"password"`
	DB       int    `yaml:"db"`
}

type MySQLConfig struct {
	Host      string `yaml:"host"`
	Port      int    `yaml:"port"`
	User      string `yaml:"user"`
	Password  string `yaml:"password"`
	DBName    string `yaml:"dbname"`
	Charset   string `yaml:"charset"`
	ParseTime bool   `yaml:"parseTime"`
}

type HTTPConfig struct {
	Address string `yaml:"address"`
}

// Config 定义配置结构体
type Config struct {
	Redis *RedisConfig `yaml:"redis"`
	MySQL *MySQLConfig `yaml:"mysql"`
	HTTP  *HTTPConfig  `yaml:"http"`
	Log   string       `yaml:"log"`
}

// LoadConfig 从文件加载配置
func SetConfig(filePath string) (*Config, error) {
	file, err := os.ReadFile(filePath)
	if err != nil {
		slog.Error("读取配置文件失败: ", "error", err)
		return nil, err
	}
	var config Config
	err = yaml.Unmarshal(file, &config)
	if err != nil {
		slog.Error("解析配置文件失败: ", "error", err)
		return nil, err
	}
	return &config, nil
}
